package com.xc.freeapp.account.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Messages;
import com.xc.freeapp.entity.UserNotification;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.MessagesService;
import com.xc.freeapp.service.impl.UserNotificationService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.TokenAnnotation;

@Controller
@RequestMapping("/userNotification")
@Api(value = "/userNotification",description="通知中心")
public class UserNotificationController extends AbstractController {
	@Autowired
	private UserNotificationService userNotificationService;
	/**
	 * 消息
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "通知列表")
	public ResponsePage<List<UserNotification>> getMessage(@RequestParam(value = "page", required = false, defaultValue="1") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue="15") Integer limit) throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("消息记录" + authInfo.getUserId() + "访问");
		PageBounds pageBounds = new PageBounds(page, limit);
		List<UserNotification> userNotifications =  userNotificationService.selectByUserId(authInfo.getUserIntId(), pageBounds);
		Paginator paginator =((PageList)userNotifications).getPaginator();
		 
		return new ResponsePage<List<UserNotification>>(userNotifications, paginator);
	}
	/**
	 * 消息
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取通知详情")
	public UserNotification getUserNotification(@PathVariable("id") Integer id) throws BaseException {
 		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("消息记录" + authInfo.getUserId() + "访问");
		UserNotification userNotification = userNotificationService.selectByid(id);

		return userNotification;
	}
	/**
	 * 消息
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "setIsRead", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "设为已读")
	public ResponseBean updateUserNotification(@RequestParam("id") Integer id) throws BaseException {
 		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("消息记录" + authInfo.getUserId() + "访问");
		userNotificationService.updateisRead(id);

		return ResponseBean.getSuccess();
	}
	/**
	 * 消息删除
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "deletelist", method = RequestMethod.PUT)
	@ResponseBody
	@ApiOperation(value = "通知删除多条")
	public ResponseBean delUserNotification(@RequestBody List<Integer> userNotificationList)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("通知记录删除" + authInfo.getUserId() + "访问");
		for (Integer id : userNotificationList) {
			userNotificationService.deleteByPrimaryKey(id);
		}
		
		return ResponseBean.getSuccess();
	}
	/**
	 * 消息删除
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	@ApiOperation(value = "通知删除多条")
	public ResponseBean delUserNotification(@PathVariable Integer id)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("通知记录删除" + authInfo.getUserId() + "访问");
		
			userNotificationService.deleteByPrimaryKey(id);

		
		return ResponseBean.getSuccess();
	}
	/**
	 * 修改消息状态
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "updatelist", method = RequestMethod.PUT)
	@ResponseBody
	@ApiOperation(value = "修改通知状态多条")
	public ResponseBean upaUserNotification(@RequestBody List<Integer> userNotificationList)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		for (Integer id : userNotificationList) {
			userNotificationService.updateisRead(id);
		}
		return ResponseBean.getSuccess();
	}
	
}
